Page({
  data: {
    scrollTop: 0,
    pageIndex: 1,
    modal: false,
    noMore: false,
    loading: false,
    imgs: {
      1: 'notice',
      2: 'remind',
      3: 'bubbles'
    },
    actions: [
      {
        name: '删除',
        color: '#80848F',
        fontsize: '22',
        width: 80,
        //icon: 'like.png',//此处为图片地址
        background: '#F7F7F7'
      }, {
        name: '已读',
        color: '#FFF',
        fontsize: '22',
        width: 80,
        //icon: 'like.png',//此处为图片地址
        background: '#ED3F14'
      }
    ]
  },
  onLoad: function (options) {
    this.setData({
      type: options.type
    });
    this.getMessages();
  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },
  onPullDownRefresh: function () {
    this.setData({
      pageIndex: 1,
      noMore: false,
      msgs: []
    });
    this.getMessages();
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    if (this.data.noMore) return;
    this.setData({
      loading: true
    });
    this.getMessages();
  },
  getMessages: function () {
    wx.util.request('', {
      query: `page=${this.data.pageIndex}${this.data.type ? '&type=' + this.data.type : ''}`
    }).then(msgs => {
      let list = this.data.msgs || [];
      for (let item of msgs) {
        item.date = wx.time.toDate(item.updated_at);
        list.push(item);
      }
      this.setData({
        msgs: list,
        pageIndex: this.data.pageIndex + 1,
        noMore: msgs.length < 10,
        loading: false
      });
    });
  },
  operate: function (e) {
    let id = e.detail.item.notify_id,
        del = (e.detail.index === 0),
        api = del ? 'del' : 'read',
        data = del ? {body: {notify_ids: `${id}`}} : {query: `notify_id=${id}`},
        idx = e.currentTarget.dataset.index,
        list = this.data.msgs;
    if (!del && list[idx].is_read == 1) {
      wx.util.toast('无需更新');
      return; 
    }
    wx.util.request(`message.${api}`, data).then(res => {
      if (del) {
        list.splice(idx, 1);
      } else {
        list[idx].is_read = 1;
      }
      this.setData({
        msgs: list
      });
    });
  },
  target: function (e) {
    let idx = e.currentTarget.dataset.index,
        msg = this.data.msgs[idx],
        url = '';
    // 更新已读状态
    if (msg.is_read == 0) {
      wx.util.request('message.read', {
        query: `notify_id=${msg.notify_id}`
      }).then(() => {
        this.setData({
          [`msgs[${idx}].is_read`]: 1
        });
      });
    }
    if (msg.type == 1) {
      // 公告详情
      url = `../news/detail?id=${msg.notify_id}`
    } else {
      let id = msg.notify.target_id;
      switch (msg.notify.target_type) {
        case 'order_consign' || 'order_stock_up_accomplish': // 订单详情
          url = `../order/detail?id=${id}`
          break;
        case 'coupon_give': // 优惠券详情
          url = `../coupon/detail?id=${id}`
          break;
      }
    }
    if (url) {
      wx.navigateTo({
        url: url,
      });
    } else {
      this.setData({
        modal: true,
        content: msg.notify.content
      });
    }
  },
  hideModal: function () {
    this.setData({
      modal: false
    });
  }
})